import time
import os
import random
import base64
import re



class ShareFunc:
    @staticmethod
    def sleep(sec):
        time.sleep(sec)
        
    @staticmethod
    def sleepex(sec):
        i=0
        while i<sec:
            time.sleep(1)
            print('.')
            i += 1
            
    @staticmethod
    def replaceXmlEncoding(xmstr=None,charset="utf-8"):
        if type(xmstr) is not str:
            return xmstr
        if re.match(r"[\s]*<\?xml",xmstr)==None:
            return xmstr
        return re.sub("encoding=\"[\w\-\s]+\"", "encoding=\""+charset+"\"", xmstr, 1)
        
    
    @staticmethod
    def getDate():
        import time
        content = time.asctime().split()
        return content[1]+ ' ' +content[2]
    
    @staticmethod
    def PrepareFile(filePath, content=None):
        if not os.path.exists(filePath):
            f = open(filePath, 'w')
            if not content:
                f.write('test text:' + filePath)
            else:
                f.write(content)
            f.close()
    
    @staticmethod
    def getRandomString(n): 
        st = ''
        for i in range(n+1):
            st = st.join(['',chr(97+random.randint(0,25))])
        return st
    
    @staticmethod
    def Base64Encode(text):
        return base64.encodestring(text)
    
    @staticmethod
    def Base64Decode(text):
        return base64.decodestring(text) 

